package com.aurora.oa.mycenter.presenter;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.aurora.oa.Constants.AuroraConstants;
import com.aurora.oa.bean.HttpResult;
import com.aurora.oa.listener.OnAddContactsGroupListener;
import com.aurora.oa.listener.OnAddTeamListener;
import com.aurora.oa.listener.OnRequestHttpDataListener;
import com.aurora.oa.model.RequestHttpModel;
import com.aurora.oa.model.RequestHttpModelImpl;
import com.aurora.oa.mycenter.bean.UserCenterData;
import com.aurora.oa.mycenter.view.MyCenterFragmentView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;

/**
* Created by Administrator on 2017/03/16
*/

public class MyCenterFragmentPresenterImpl implements MyCenterFragmentPresenter{

    private Handler mHandler;
    private RequestHttpModel requestHttpModel;
    private MyCenterFragmentView view;
    public MyCenterFragmentPresenterImpl(Handler mHandler) {
        requestHttpModel = new RequestHttpModelImpl();
        this.mHandler = mHandler;
    }

    public MyCenterFragmentPresenterImpl(MyCenterFragmentView view) {
        requestHttpModel = new RequestHttpModelImpl();
        this.view = view;
    }

    @Override
    public void getMyCenterDataHttp() {
        requestHttpModel.requestHttpDataString(AuroraConstants.GET_USER_CURRENT_MONTH_DATA, new OnRequestHttpDataListener(){

            @Override
            public void onRequestDataSuccess(String body) {
                Gson gson = new Gson();
                Type type = new TypeToken<UserCenterData>(){}.getType();
                UserCenterData result= gson.fromJson(body,type);
                view.showUserCurrentMonthData(result);
   /*             Message msg =Message.obtain();
                msg.what = 0;
                msg.obj = userCenterData;
                mHandler.sendMessage(msg);*/
            }

            @Override
            public void onRequestDataFail(String message) {
                view.showMessage(message);
            }
        });

    }

    @Override
    public void creatTeam(String teamName) {
        view.showProgressDialog("正在创建团队");
        requestHttpModel.doAddTeam(teamName, new OnAddTeamListener() {
            @Override
            public void onAddTeamSuccess() {
                view.disMissProgressDialog();
                view.showMessage("团队创建成功");
            }

            @Override
            public void onAddTeamFail(String message) {
                view.disMissProgressDialog();
                view.showMessage(message);
            }
        });
    }

    @Override
    public void creatContacts(String contactsName) {
        view.showProgressDialog("正在创建通讯录");
        requestHttpModel.doAddContactsGroup(contactsName, new OnAddContactsGroupListener() {
            @Override
            public void onAddContactsGroupSuccess() {
                view.disMissProgressDialog();
                view.showMessage("通讯录创建成功");
            }

            @Override
            public void onAddContactsGroupFail(String message) {
                view.disMissProgressDialog();
                view.showMessage(message);
            }
        });
    }
}